package com.laconic.vbenadmin.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.laconic.vbenadmin.SingleResponse;
import com.laconic.vbenadmin.bean.WeatherCity;
import com.laconic.vbenadmin.bean.WeatherRequest;
import com.laconic.vbenadmin.bean.WeatherResponse;
import com.laconic.vbenadmin.config.GaoDeConfig;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @Author JiangLiu
 * @Date 2025/5/26 15:52
 **/
@CrossOrigin
@RestController
@RequestMapping("/api")
public class WeatherController {


    @Autowired
    private GaoDeConfig gaoDeConfig;

    // 查询城市
    // 通过elastic-search查询
    @GetMapping("/city/all")
    public SingleResponse<List<WeatherCity>> getCityList(HttpServletRequest httpServletRequest) {
        return null;
    }
    @PostMapping("/find/city/weather")
    public SingleResponse<WeatherResponse> getCityWeather(HttpServletRequest httpServletRequest) throws JsonProcessingException {
        OkHttpClient client = new OkHttpClient();
        // 构建请求对象
        WeatherRequest query = new WeatherRequest(gaoDeConfig.getKey(), "130726", "all", "JSON");

        System.out.println(gaoDeConfig.getUrl());
        HttpUrl url = HttpUrl.parse(gaoDeConfig.getUrl())
                .newBuilder()
                .addQueryParameter("key", query.getKey())
                .addQueryParameter("city", query.getCity())
                .addQueryParameter("extensions", query.getExtensions())
                .addQueryParameter("output", query.getOutput())
                .build();

        // 构建请求
        Request request = new Request.Builder()
                .url(url)
                .get()
                .addHeader("Accept", "application/json")
                .build();

        // 发起请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
                WeatherResponse info = JSONObject.parseObject(responseBody, WeatherResponse.class);
                return SingleResponse.success(info);
            } else {
                System.err.println("请求失败: " + response.code());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
